<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事件名称" prop="cEventName">
        <el-input
          v-model="queryParams.cEventName"
          placeholder="请输入事件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事件描述" prop="cEventDescribe">
        <el-input
          v-model="queryParams.cEventDescribe"
          placeholder="请输入事件描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事件类别" prop="cEventCategory">
        <el-input
          v-model="queryParams.cEventCategory"
          placeholder="请输入事件类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发生时间" prop="cEventTime">
        <el-date-picker clearable
          v-model="queryParams.cEventTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发生时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发生地点" prop="cEventLocation">
        <el-input
          v-model="queryParams.cEventLocation"
          placeholder="请输入发生地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="办结时间" prop="cEventCompletedTime">
        <el-date-picker clearable
          v-model="queryParams.cEventCompletedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择办结时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属地区id" prop="cEventDistrictId">
        <el-input
          v-model="queryParams.cEventDistrictId"
          placeholder="请输入所属地区id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属地区名称" prop="cEventDistrictName">
        <el-input
          v-model="queryParams.cEventDistrictName"
          placeholder="请输入所属地区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="cLongitude">
        <el-input
          v-model="queryParams.cLongitude"
          placeholder="请输入经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维度" prop="cLatitude">
        <el-input
          v-model="queryParams.cLatitude"
          placeholder="请输入维度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事件影响范围(公里)" prop="cEventReach">
        <el-input
          v-model="queryParams.cEventReach"
          placeholder="请输入事件影响范围(公里)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重伤人数" prop="cEventInjuriesNum">
        <el-input
          v-model="queryParams.cEventInjuriesNum"
          placeholder="请输入重伤人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="死亡人数" prop="cEventDieNum">
        <el-input
          v-model="queryParams.cEventDieNum"
          placeholder="请输入死亡人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="直接经济损失" prop="cDirectEconomicLosses">
        <el-input
          v-model="queryParams.cDirectEconomicLosses"
          placeholder="请输入直接经济损失"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:eventManage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:eventManage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:eventManage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:eventManage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventManageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事件id" align="center" prop="cEventId" />
      <el-table-column label="子事件ID集合" align="center" prop="cSubEventList" />
      <el-table-column label="事件名称" align="center" prop="cEventName" />
      <el-table-column label="事件描述" align="center" prop="cEventDescribe" />
      <el-table-column label="事件类别" align="center" prop="cEventCategory" />
      <el-table-column label="事件类型" align="center" prop="cEventType" />
      <el-table-column label="事件状态" align="center" prop="cEventStatus" />
      <el-table-column label="发生时间" align="center" prop="cEventTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cEventTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发生地点" align="center" prop="cEventLocation" />
      <el-table-column label="办结时间" align="center" prop="cEventCompletedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cEventCompletedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属地区id" align="center" prop="cEventDistrictId" />
      <el-table-column label="所属地区名称" align="center" prop="cEventDistrictName" />
      <el-table-column label="经度" align="center" prop="cLongitude" />
      <el-table-column label="维度" align="center" prop="cLatitude" />
      <el-table-column label="事件影响范围(公里)" align="center" prop="cEventReach" />
      <el-table-column label="重伤人数" align="center" prop="cEventInjuriesNum" />
      <el-table-column label="死亡人数" align="center" prop="cEventDieNum" />
      <el-table-column label="直接经济损失" align="center" prop="cDirectEconomicLosses" />
      <el-table-column label="事件视频链接" align="center" prop="cEventVideoUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:eventManage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:eventManage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改事件管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="子事件ID集合" prop="cSubEventList">
          <el-input v-model="form.cSubEventList" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="事件名称" prop="cEventName">
          <el-input v-model="form.cEventName" placeholder="请输入事件名称" />
        </el-form-item>
        <el-form-item label="事件描述" prop="cEventDescribe">
          <el-input v-model="form.cEventDescribe" placeholder="请输入事件描述" />
        </el-form-item>
        <el-form-item label="事件类别" prop="cEventCategory">
          <el-input v-model="form.cEventCategory" placeholder="请输入事件类别" />
        </el-form-item>
        <el-form-item label="发生时间" prop="cEventTime">
          <el-date-picker clearable
            v-model="form.cEventTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发生时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发生地点" prop="cEventLocation">
          <el-input v-model="form.cEventLocation" placeholder="请输入发生地点" />
        </el-form-item>
        <el-form-item label="办结时间" prop="cEventCompletedTime">
          <el-date-picker clearable
            v-model="form.cEventCompletedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择办结时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所属地区id" prop="cEventDistrictId">
          <el-input v-model="form.cEventDistrictId" placeholder="请输入所属地区id" />
        </el-form-item>
        <el-form-item label="所属地区名称" prop="cEventDistrictName">
          <el-input v-model="form.cEventDistrictName" placeholder="请输入所属地区名称" />
        </el-form-item>
        <el-form-item label="经度" prop="cLongitude">
          <el-input v-model="form.cLongitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="维度" prop="cLatitude">
          <el-input v-model="form.cLatitude" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="事件影响范围(公里)" prop="cEventReach">
          <el-input v-model="form.cEventReach" placeholder="请输入事件影响范围(公里)" />
        </el-form-item>
        <el-form-item label="重伤人数" prop="cEventInjuriesNum">
          <el-input v-model="form.cEventInjuriesNum" placeholder="请输入重伤人数" />
        </el-form-item>
        <el-form-item label="死亡人数" prop="cEventDieNum">
          <el-input v-model="form.cEventDieNum" placeholder="请输入死亡人数" />
        </el-form-item>
        <el-form-item label="直接经济损失" prop="cDirectEconomicLosses">
          <el-input v-model="form.cDirectEconomicLosses" placeholder="请输入直接经济损失" />
        </el-form-item>
        <el-form-item label="事件视频链接" prop="cEventVideoUrl">
          <el-input v-model="form.cEventVideoUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">事件跟踪信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTEventTrack">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTEventTrack">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tEventTrackList" :row-class-name="rowTEventTrackIndex" @selection-change="handleTEventTrackSelectionChange" ref="tEventTrack">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="单位" prop="cUnit" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cUnit" placeholder="请输入单位" />
            </template>
          </el-table-column>
          <el-table-column label="事件" prop="cTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.cTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择事件" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEventManage, getEventManage, delEventManage, addEventManage, updateEventManage } from "@/api/system/eventManage";

export default {
  name: "EventManage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTEventTrack: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 事件管理表格数据
      eventManageList: [],
      // 事件跟踪表格数据
      tEventTrackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cSubEventList: null,
        cEventName: null,
        cEventDescribe: null,
        cEventCategory: null,
        cEventType: null,
        cEventStatus: null,
        cEventTime: null,
        cEventLocation: null,
        cEventCompletedTime: null,
        cEventDistrictId: null,
        cEventDistrictName: null,
        cLongitude: null,
        cLatitude: null,
        cEventReach: null,
        cEventInjuriesNum: null,
        cEventDieNum: null,
        cDirectEconomicLosses: null,
        cEventVideoUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询事件管理列表 */
    getList() {
      this.loading = true;
      listEventManage(this.queryParams).then(response => {
        this.eventManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cEventId: null,
        cSubEventList: null,
        cEventName: null,
        cEventDescribe: null,
        cEventCategory: null,
        cEventType: null,
        cEventStatus: null,
        cEventTime: null,
        cEventLocation: null,
        cEventCompletedTime: null,
        cEventDistrictId: null,
        cEventDistrictName: null,
        cLongitude: null,
        cLatitude: null,
        cEventReach: null,
        cEventInjuriesNum: null,
        cEventDieNum: null,
        cDirectEconomicLosses: null,
        cEventVideoUrl: null
      };
      this.tEventTrackList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cEventId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加事件管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cEventId = row.cEventId || this.ids
      getEventManage(cEventId).then(response => {
        this.form = response.data;
        this.tEventTrackList = response.data.tEventTrackList;
        this.open = true;
        this.title = "修改事件管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tEventTrackList = this.tEventTrackList;
          if (this.form.cEventId != null) {
            updateEventManage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEventManage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cEventIds = row.cEventId || this.ids;
      this.$modal.confirm('是否确认删除事件管理编号为"' + cEventIds + '"的数据项？').then(function() {
        return delEventManage(cEventIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 事件跟踪序号 */
    rowTEventTrackIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 事件跟踪添加按钮操作 */
    handleAddTEventTrack() {
      let obj = {};
      obj.cUnit = "";
      obj.cContent = "";
      obj.cTime = "";
      this.tEventTrackList.push(obj);
    },
    /** 事件跟踪删除按钮操作 */
    handleDeleteTEventTrack() {
      if (this.checkedTEventTrack.length == 0) {
        this.$modal.msgError("请先选择要删除的事件跟踪数据");
      } else {
        const tEventTrackList = this.tEventTrackList;
        const checkedTEventTrack = this.checkedTEventTrack;
        this.tEventTrackList = tEventTrackList.filter(function(item) {
          return checkedTEventTrack.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTEventTrackSelectionChange(selection) {
      this.checkedTEventTrack = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/eventManage/export', {
        ...this.queryParams
      }, `eventManage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
